package com.ecarx.vhaltests.hy11.testcases.drivemode;


import static vendor.ecarx.xma.automotive.vehicle.V2_0.ECarXVehicleProperty.DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import vendor.ecarx.xma.automotive.vehicle.V2_0.ECarXVehicleProperty;

//SW_FC_041_014 投影模式-梯形校正手动校正
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DriveModeProjectionTrapCorrectManualSetTest extends VhalUnitTestBase {

    private static final String TAG = "DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET";

    private static IpcpClient ipcpClient = new IpcpClient();
    
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static int[] halValue;
    private static int halStatus;
    final static Object lock = new Object();

    @BeforeClass
    public static void testInit() {
        //mCarPropertyMgr.registerCallback(carPropertyEventCallback, DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig();
    }

    /*@Test
    public void testProjectionTrapCorrectManualSetAvailable() {
        setProjectionTrapCorrectManualSetAvailable();
        lockWait();
        testProjectionTrapCorrectManualSetAvai();
        delay();
    }

    @Test
    public void testProjectionTrapCorrectManualSetNotAvailable() {
        setProjectionTrapCorrectManualSetNotAvailable();
        lockWait();
        testProjectionTrapCorrectManualSetNotAvai();
        delay();
    }*/

    @Test
    public void testProjectionTrapCorrectManualSetLeftUpPosUP() {
        setmanTrapCorrn(1,1,0,0,0,0,0,0);
        delay();
        setProjectionTrapCorrectManualSetAvailable();
        delay();
        setmanTrapCorrnSet(1, 0, 0, 0);
        lockWait();
        //Assert.assertEquals(halValue, 0);
        //testProjectionTrapCorrectManualSetValue(0);//
        CarPropertyValue<Integer[]> halValue = mCarPropertyMgr.getProperty(Integer[].class, DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, AREA_ID);
        Integer[] values = halValue.getValue();
        for (int i = 0; i < values.length; i++) {
            Log.d(TAG, "########### testProjectionTrapCorrectManualSetLeftUpPosUP halStatus[]= ：" + values[i]);
        }
    }

    @Test
    public void testProjectionTrapCorrectManualSetRightDownPosLEFT() {
        setmanTrapCorrn(0,0,0,0,0,0,1,1);   
        delay();
        setProjectionTrapCorrectManualSetAvailable();
        delay();
        setmanTrapCorrnSet(0, 0, 0, 4);
        lockWait();
        //Assert.assertEquals(halValue, 1);
        //testProjectionTrapCorrectManualSetValue(1);//
        CarPropertyValue<Integer[]> halValue = mCarPropertyMgr.getProperty(Integer[].class, DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, AREA_ID);
        Integer[] values = halValue.getValue();
        for (int i = 0; i < values.length; i++) {
            Log.d(TAG, "########### testProjectionTrapCorrectManualSetRightDownPosLEFT halStatus[]= ：" + values[i]);
        }
    }
   
    private void setProjectionTrapCorrectManualSetAvailable(){
        setPrjtSwtFbAndAutTrapCorrFb(1, 0);
    }

    private void setProjectionTrapCorrectManualSetNotAvailable(){
        setPrjtSwtFbAndAutTrapCorrFb(0, 0);
    }

    private static void setConfig() {
        int ccSvcId = 135;
        int[] ccOperIds = {200};

        int cemSvcId = 37;
        int[] cemOperIds = {999};

        int vddmSvcId = 48;
        int[] vddmOperIds = {999};

        int fplSvcId = 67;
        int[] fplOperIds = {999};

        ipcpClient.setConfig(ccSvcId, ccOperIds);
        ipcpClient.setConfig(cemSvcId, cemOperIds);
        ipcpClient.setConfig(vddmSvcId, vddmOperIds);
        ipcpClient.setConfig(fplSvcId, fplOperIds);

        int[] configs = {745};
        int[] values = {0};
        boolean ccRet = ipcpClient.setCarconfigSignal(ccSvcId, ccOperIds[0], configs, values);
        Assert.assertTrue(ccRet);

    }

    private void setPrjtSwtFbAndAutTrapCorrFb(int value, int value2){
        int fplSvcId = 67;
        int[] fplOperIds = {999};

        String[] fpls = {"prjtSwtFb", "autTrapCorrFb"};
        int[] fplValues = {value, value2};
        boolean fplsRet = ipcpClient.setFPLSignal(fplSvcId, fplOperIds[0], fpls, fplValues);
        Assert.assertTrue(fplsRet);
    }

    private void setmanTrapCorrn(int value1, int value2, int value3, int value4, int value5, int value6, int value7, int value8){
        int fplSvcId = 67;
        int[] fplOperIds = {999};

        String[] fpls = {"manTrapCorrnLeUpPosnUpDwnPosn","manTrapCorrnLeUpPosnLeRiPosn", "manTrapCorrnLeDwnPosnUpDwnPosn","manTrapCorrnLeDwnPosnLeRiPosn", 
                         "manTrapCorrnRiUpPosnUpDwnPosn", "manTrapCorrnRiUpPosnLeRiPosn", "manTrapCorrnRiDwnPosnUpDwnPosn", "manTrapCorrnRiDwnPosnLeRiPosn"};
        int[] fplValues = {value1, value2, value3, value4, value5, value6, value7, value8};
        boolean fplsRet = ipcpClient.setFPLSignal(fplSvcId, fplOperIds[0], fpls, fplValues);
        Assert.assertTrue(fplsRet);
    }
    
    private static Integer[] toIntegerArray(int[] input) {
        int len = input.length;
        Integer[] arr = new Integer[len];

        for (int i = 0; i < len; ++i) {
            arr[i] = input[i];
        }
        return arr;
    }
    
    private void setmanTrapCorrnSet(int leftUpPos, int leftDownPos, int rightUpPos, int rightDownPos){
        int[] value = new int[4]; 
        value[0] = leftUpPos;
        value[1] = leftDownPos;  
        value[2] = rightUpPos;  
        value[3] = rightDownPos;                              
        mCarPropertyMgr.setProperty(Integer[].class, DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, AREA_ID, toIntegerArray(value));
    }
    
    private void testProjectionTrapCorrectManualSetAvai(){
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, AREA_ID);
        Assert.assertTrue(isAvail);
    }

    private void testProjectionTrapCorrectManualSetNotAvai(){
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(DRIVEMODE_PROJECTION_TRAP_CORRECT_MANUAL_SET, AREA_ID);
        Assert.assertFalse(isAvail);
    }

   /* private static final CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                Log.d(TAG, carPropertyValue.toString());
                halValue = (int[])carPropertyValue.getValue();
                halStatus = carPropertyValue.getStatus();
                synchronized (lock) {
                    lock.notify();
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };*/

    public void lockWait() {
        try {
            synchronized (lock) {
                lock.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
